package oceanlife;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Polygon;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.geom.Ellipse2D;
import java.awt.geom.Line2D;
import java.awt.geom.Rectangle2D;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

import oceanlife.pathfinding.DelaunayTriangleTranslator;
import oceanlife.pathfinding.Edge;
import oceanlife.pathfinding.Line2;
import oceanlife.pathfinding.Obstacles;
import oceanlife.pathfinding.PathTriangle;
import oceanlife.pathfinding.Pnt;
import oceanlife.pathfinding.PointGraph;
import oceanlife.pathfinding.RadialFace;
import oceanlife.pathfinding.SimpleObstacle;
import oceanlife.pathfinding.Triangle;



public class TestPanel extends JPanel{
	public ArrayList<Line2> lines = new ArrayList<Line2>();
	public ArrayList<Line2> linesAfter = new ArrayList<Line2>();
	public HashSet<Line2> linesConnect = new HashSet<Line2>();
	public ArrayList<Pnt> pnts = new ArrayList<Pnt>();
	
	
	public void init() throws Exception{
		//DelaunayTriangleTranslator dtt = new DelaunayTriangleTranslator();
		ArrayList<SimpleObstacle> squares = new ArrayList<SimpleObstacle>();
		ArrayList<Pnt> ob = new ArrayList<Pnt>();
		ob.add(new Pnt(0.0,50.0));
		ob.add(new Pnt(0.0,150.0));
		ob.add(new Pnt(150.0,150.0));
		ob.add(new Pnt(150.0,50.0));
		squares.add(new SimpleObstacle(ob));
		ob = new ArrayList<Pnt>();
		ob.add(new Pnt(210.0,50.0));
		ob.add(new Pnt(210.0,150.0));
		ob.add(new Pnt(300.0,150.0));
		ob.add(new Pnt(300.0,50.0));
		squares.add(new SimpleObstacle(ob));
		ob = new ArrayList<Pnt>();
		ob.add(new Pnt(50.0,260.0));
		ob.add(new Pnt(50.0,400.0));
		ob.add(new Pnt(240.0,400.0));
		ob.add(new Pnt(240.0,280.0));
		squares.add(new SimpleObstacle(ob));
		ob = new ArrayList<Pnt>();
		//HashMap<Pnt, SimpleObstacle> obsRepos = new HashMap<Pnt, SimpleObstacle>();
		for(SimpleObstacle so : squares){
			//obsRepos.put(so.getCentre(), so);
			//dtt.addSite(so.getCentre());
			//pnts.add(so.getCentre());
			lines.addAll(so.getObstacleEdges());
		}
		for(SimpleObstacle so : squares){
			so.createSurroundingPathLines();
			linesAfter.addAll(so.getObstacleEdges());
		}
		//create lines connecting all points from polygons
		//for each point
		HashSet<SimpleObstacle> done = new HashSet<SimpleObstacle>();
		for(SimpleObstacle so1 : squares){
			done.add(so1);
			for(Pnt corner1 : so1.getObstacleCorners()){
				//to each other point
				for(SimpleObstacle so2 : squares){
					if(done.contains(so2)){
						continue;
					}
					for(Pnt corner2 : so2.getObstacleCorners()){
						//check each obstacle
						boolean overlap = false;
						overlapCheck:
						for(SimpleObstacle soOverlap : squares){
							//if(soOverlap.equals(so1)){
							//	continue;
							//}
							for(Line2 lineOverlap: soOverlap.getObstacleEdges()){
								Pnt pnt = new Line2(corner1,corner2).intersectionPoint(lineOverlap);
								if(pnt != null && !(pnt.equals(corner1)||pnt.equals(corner2))){ // && !(pnt.equals(corner1)||pnt.equals(corner2)) 
									overlap = true;
									break overlapCheck;
								}
							}
						}
						if(!overlap){
							linesConnect.add(new Line2(corner1,corner2));
						}
					}
				}
			}
		}
		
		
		//for(SimpleObstacle so : SimpleObstacle.getInitObstacles()){
			//obsRepos.put(so.getCentre(), so);
			//dtt.addSite(so.getCentre());
		//}	
		//HashSet<Pnt> done = new HashSet<Pnt>(DelaunayTriangleTranslator.initialTriangle);
	    //for (Triangle triangle : dtt.getDT()){
	    	//PathTriangle pt = new PathTriangle(triangle, obsRepos);
	    	//ArrayList<Line2> triLines = triangle.getEdges();
	    	
	    	//lines.addAll(triLines);	
	    	//lines.addAll(pt.getPathLines());	
/*	        for (Pnt site: triangle) {
	            if (done.contains(site)) continue;
	            done.add(site);
	            List<Triangle> list = dtt.getDT().surroundingTriangles(site, triangle);
	            Pnt[] vertices = new Pnt[list.size()];
	            ArrayList<Pnt> potLines = new ArrayList<Pnt>();
	            int i = 0;
	            Pnt first = null;
	            for (Triangle tri: list){
	                vertices[i++] = tri.getCircumcenter();
                    potLines.add(tri.getCircumcenter());
                    if(i == 1){
                    	first = tri.getCircumcenter();
	                }
	            	Line2 line = null;
	                if(potLines.size() == 2){
	                	line = new Line2(potLines.get(0),potLines.get(1));
	                    potLines.remove(0);
	                }
	                if(first != null && i == list.size() && potLines.size() == 1){
	                	line = new Line2(potLines.get(0),first);
	                }
	                if(line != null && !lines.contains(line)){
	                	lines.add(line);
	                }
	                System.out.println(line);
	            }
	            //draw(vertices, withFill? getColor(site) : null);
	            //if (withSites) draw(site);
	        }*/
	    //}
	}
	
	public void paintComponent(Graphics g) {
		super.paintComponent(g);
		Graphics2D g2d = (Graphics2D)g;
		for(Line2 line : lines){
			g2d.draw(line);
		}
		g2d.setColor(Color.green);
		for(Line2 line : linesAfter){
			g2d.draw(line);
		}
		g2d.setColor(Color.red);
		for(Line2 line : linesConnect){
			g2d.draw(line);
		}
		for(Pnt pnt : pnts){
			//System.out.println(pnt);
			g2d.draw(new Ellipse2D.Double(pnt.coord(0)-3,pnt.coord(1)-3,6,6));
		}
	}
	
	public static void main(String[] argv) throws Exception{
		final JFrame f = new JFrame();
		f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		f.getContentPane().setLayout(null);
		f.setPreferredSize(new Dimension(800,500));
		final TestPanel tp = new TestPanel();
		tp.init();
		
		tp.setBounds(0, 0, 800, 500);
		f.getContentPane().add(tp);
        f.pack();
        f.setVisible(true);

	}
}

